package be.bejug.setris

import blocks.Blocks
import swinggui.SetrisFrame
import swingtools.SwingTools

/**
 * User: Peter
 * Date: 30-mei-2010
 * Time: 11:46:13
 */


class TestGame(private val gui: SetrisGui) extends SetrisGame {
  private val ticksPerSecond = 1
  private var playField = new TileGrid(gui.playFieldDimension._1, gui.playFieldDimension._2)
  private var blockList = Blocks.allBlocks.toList
  private var tilesList = Tiles.allTiles.toList
  private var score = 0
  private var level = 0
  private var index = 0
  private var yPos = 0
  private var xPos = 0
  private var block = Blocks.randomBlock
  private var nextBlock = Blocks.randomBlock
  private var dropped = false
  gui.setGame(this)
  gui.setNextBlock(nextBlock)


  def moveToNextBlock(playField: TileGrid): TileGrid = {
       //TODO Remove full lines
//    while(playField.detectFullLine(yPos + block.height)){
//      playField.
//    }
    if (yPos == 0) gui.setGameOver(true)
    block = nextBlock
    nextBlock = Blocks.randomBlock
    gui.setNextBlock(nextBlock)
    yPos = 0
    this.playField = playField
    xPos = (playField.width - block.width) / 2
    if (!validatePosition(xPos, yPos, block))
      gui.setGameOver(true)    
    playField
  }

  def tick(tickCount: Long) {
    //if(tickCount % (gui.frameRate/ticksPerSecond) != 0) return
    //			score += 10;
    //			level += 1;
    //			gui.setLevel(level)
    //			gui.setScore(score)
    //block = blockList.head

    if (yPos == 0)
      xPos = (playField.width - block.width) / 2

    val playFieldScreen =
    if (playField.collision(xPos, yPos, block))
      moveToNextBlock(playField.draw(xPos, yPos - 1, block))
    else
      playField.draw(xPos, yPos, block)

    if (dropped || tickCount % (gui.frameRate / ticksPerSecond) == 0) {
        if (yPos < playField.height - block.height)
          yPos += 1
        else {
          moveToNextBlock(playFieldScreen)
        }
      }
    //			gui.setNextBlock(blockList.head)
    //			val	y	=	(index / playField.width) % ( playField.height)
    //			val x	=	index % playField.width
    //			playField	=	playField.set(x,y,Some(tilesList.head))
    gui.setPlayField(playFieldScreen)
    //			index += 1
    //			blockList = blockList.tail.:+(blockList.head.rotateRight);
    //			tilesList	=	tilesList.tail :+ tilesList.head
    //			if(index > playField.width * playField.height){
    //gui.setGameOver(true)

    //			}


  }

  def start: Unit = {
    playField = playField.clear
    index = 0
    score = 0
    level = 0
    gui.setGameOver(false)
  }

  def dropBlock {
    dropped = !dropped
  }

  def validatePosition(xPos:Int, yPos:Int, block:TileGrid) = {
    playField.width >= xPos + block.width &&
    xPos >= 0 &&
    yPos >= 0 &&
    !playField.collision(xPos, yPos, block)
  }

  def moveRight {
    if (validatePosition(xPos + 1, yPos, block))
      xPos += 1
  }

  def moveLeft {
    // if (xPos > 0 && !playField.collision(xPos - 1, yPos, block))
    if (validatePosition(xPos - 1, yPos, block))
      xPos -= 1
    println("moveLeft")
  }

  def rotateRight = {
    val rotBlock = block.rotateRight
    //if (!playField.collision(xPos, yPos, rotBlock))
    if (validatePosition(xPos, yPos, rotBlock)) {
      block = rotBlock
    }
  }

  def rotateLeft = {
    val rotBlock = block.rotateLeft
    // if (!playField.collision(xPos, yPos, rotBlock))
    if (validatePosition(xPos, yPos, rotBlock)) {
      block = rotBlock
    }
  }
}

object GuiTest {
  import SwingTools._
  def main(args: Array[String]) = swingRunLater {
    val gui = new SetrisFrame
    val game = new TestGame(gui)

  }
}